package com.cloudcc.mobile.view.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.cloudcc.cloudframe.model.UserModel;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.UpdateImage;
import com.cloudcc.mobile.entity.UserBasicInfoForUpdate;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.util.ImageUtils;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.NetWork;
import com.cloudcc.mobile.util.Tools;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.base.BaseActivity;
import com.cloudcc.mobile.weight.CustomProgressDialog;
/**
 * 修改我的信息
 * @author Administrator
 *
 */
public class UpdateInfoActivity extends BaseActivity{
	private ImageView fanhui;
	private ImageView save;
	private EditText me_about,me_address,me_mobile,me_phone,me_email;
	private String email="";
	private String photo="";
	private String mobile="";
	private String address="";
	private String about="";
	private UserBasicInfoForUpdate mUserBasicInfo;
	private CustomProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		try {
			setupView();//修改
			setData();
			addListener();
		} catch (Exception e) {
			Tools.handle(e);
		}
	}
	@Override
	public int getLayoutId() {
		return R.layout.update_info;
	}
	/**
	 * 获取控件
	 */
	private void setupView() {
		fanhui=(ImageView)findViewById(R.id.fanhui_guanyuwo);
		save=(ImageView)findViewById(R.id.saveinfo);
		me_email=(EditText)findViewById(R.id.me_email);
		me_phone=(EditText)findViewById(R.id.me_phone);
		me_mobile=(EditText)findViewById(R.id.me_mobile);
		me_address=(EditText)findViewById(R.id.me_address);
		me_about=(EditText)findViewById(R.id.me_about);

	}
	private void setData() {
		mUserBasicInfo=(UserBasicInfoForUpdate) getIntent().getSerializableExtra("chatuser");
		me_email.setText(mUserBasicInfo.getEmail());
		me_phone.setText(mUserBasicInfo.getPhone());
		me_mobile.setText(mUserBasicInfo.getMobile());
		me_address.setText(mUserBasicInfo.getAddress());
		me_about.setText(mUserBasicInfo.getAbout());
	}
	private void addListener() {
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				updateData();
			}
		});
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				UpdateInfoActivity.this.finish();
			}
		});
	}
	protected void updateData() {
		email=me_email.getText().toString().trim();
		photo=me_phone.getText().toString().trim();
		mobile=me_mobile.getText().toString().trim();
		address=me_address.getText().toString().trim();
		about=me_about.getText().toString().trim();
		new UpdateDataTask().execute();
	}

	/**
	 * 修改数据
	 */
	class UpdateDataTask extends AsyncTask<Void, Void, String>{
		@Override
		protected void onPreExecute() {
			showWainting();
			super.onPreExecute();
		}
		@Override
		protected String doInBackground(Void... arg0) {
			String result="";
			try {
				String bitmapToBase64;
				if (mUserBasicInfo.getImg()==null) {
					bitmapToBase64="";
				}else{
					bitmapToBase64=ImageUtils.bitmapToBase64(ImageUtils.Bytes2Bimap(mUserBasicInfo.getImg()));
				}
				UpdateImage upimage=new UpdateImage(email,"",photo,mobile,
						about,"",address, "",mUserBasicInfo.getTitle(),bitmapToBase64);
				String data=Json.toJson(upimage);
				List<NameValuePair> pairs=new ArrayList<NameValuePair>();
				pairs.add(new BasicNameValuePair("serviceName", "updateChatterUserInfo"));
				pairs.add(new BasicNameValuePair("data", data));
				pairs.add(new BasicNameValuePair("binding", AppContext.binding));
				String url=UrlTools.url+UrlTools.postpath;
				result=NetWork.post(url, pairs);
				return result;
			} catch (Exception e) {
				Tools.handle(e);
			}
			return null;
		}
		@Override
		protected void onPostExecute(String result) {
			try {
				dismissWainting();
				if(result==null){
					Toast.makeText(mContext, "修改信息失败", 1).show();
					return ;
				}else{
					JSONObject json=new JSONObject(result);
					boolean isResult=json.getBoolean("result");
					if(isResult){
						onSaveSuccess();
					}else{
						Toast.makeText(mContext, "修改信息失败", 1).show();
						return ;
					}
				}
			} catch (JSONException e) {
				Tools.handle(e);
			}
			super.onPostExecute(result);
		}
	}
	private void onSaveSuccess(){
		mUserBasicInfo.setEmail(email);
		mUserBasicInfo.setPhone(photo);
		mUserBasicInfo.setMobile(mobile);
		mUserBasicInfo.setAddress(address);
		mUserBasicInfo.setAbout(about);
		Intent data =new Intent();
		data.putExtra("user.new", mUserBasicInfo);
		setResult(RESULT_OK, data);
		finish();
	}


}
